<?
class Block{
    static function Get($id, $fields = "*"){
        $filter = array('id' => $id);
        $result =  Block::GetList($filter, array(), $fields);
        return !empty($result) ? $result[0] : false;
    }
    
    static function GetList($filter = array(), $sort = array(), $fields = "*", $limit = false){
        BDS::Init();
        $obj = BDSBlock::GetList($filter, $sort, $fields, $limit);
        $result = array();
        while($block = $obj->fetch()){            
            $block['fields'] = array('element' => array(), 'section' => array());
            $sfields = FLDFields::GetList(array('block_id' => $block['id'], 'is_section' => 1))->GetList();
            $efields = FLDFields::GetList(array('block_id' => $block['id'], 'is_section' => 'NULL()'))->GetList();
            
            foreach($sfields as $sf){
                $block['fields']['section'][$sf['code']] = $sf;
            }            
            foreach($efields as $ef){
                $block['fields']['element'][$ef['code']] = $ef;
            }
            $result[] = $block;
        }
        return $result;
    }
}